package com.exchange.aspect;

import com.exchange.service.ResourceLimitService;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ResourceLimitAspect {
    
    @Autowired
    private ResourceLimitService resourceLimitService;
    
    // Pointcut for methods that add products
    @Before("execution(* com.exchange.service.ProductService.createProduct(..)) && args(product, merchantId, ..)")
    public void checkProductLimitBeforeCreate(Object product, Long merchantId) {
        if (!resourceLimitService.isWithinProductLimit(merchantId, null)) {
            throw new RuntimeException("Merchant has reached the maximum number of products");
        }
    }
    
    // Pointcut for methods that add exchange codes
    @Before("execution(* com.exchange.service.ExchangeCodeService.createExchangeCode(..)) && args(exchangeCode, merchantId, ..)")
    public void checkExchangeCodeLimitBeforeCreate(Object exchangeCode, Long merchantId) {
        if (!resourceLimitService.isWithinExchangeCodeLimit(merchantId, null)) {
            throw new RuntimeException("Merchant has reached the maximum number of exchange codes");
        }
    }
    
    // Pointcut for methods that create orders
    @Before("execution(* com.exchange.service.OrderService.createOrder(..)) && args(order, merchantId, ..)")
    public void checkOrderLimitBeforeCreate(Object order, Long merchantId) {
        if (!resourceLimitService.isWithinOrderLimit(merchantId, null)) {
            throw new RuntimeException("Merchant has reached the maximum number of orders for this month");
        }
    }
}